<template>
  <div class="app-container">
    <div v-show="!modifyInfo.visible && !detailInfo.visible">
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          ref="pageFilter"
          v-model:query="filterInfo.query.queryMap"
          :query-more-json="true"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
          :clear-type="clearType"
          :slip-select-request="slipSelectRequest"
        >
        </NewPageFilter>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btList">
          <template v-slot:filter-remove>
            <el-button icon="Delete" class="deleteBtnLight" @click="remove()"
              >删除</el-button
            >
          </template>
        </NewPageFilter>
        <div class="display-flex">
          <el-button @click="preferenceInfo.visible = true">偏好设置</el-button>
        </div>
      </div>
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :tab-index="false"
        :check-box="true"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-name="scope">
          <p class="slip-edit" @click="showDetail(scope.row)">
            {{ scope.row.name || '-' }}
          </p>
        </template>
      </TableContainer>
    </div>
    <!-- 新增或修改 -->
    <Modify
      v-if="modifyInfo.visible"
      :id="modifyInfo.id"
      :type="modifyInfo.type"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      @returnModify="returnModify"
    />
    <!-- 详情页 -->
    <Detail
      v-if="detailInfo.visible"
      :id="detailInfo.id"
      v-model:visible="detailInfo.visible"
      :title="detailInfo.title"
      @returnBack="getList"
    />
    <SetPreference
      v-if="preferenceInfo.visible"
      v-model:visible="preferenceInfo.visible"
      :search-list="filterInfo.searchList"
      :field-list="tableInfo.fieldList"
      :customize-user-v-o="filterInfo.query.customizeUserVO"
      @callback="setPreferenceCallback"
    />
  </div>
</template>

<script setup name="deviceMaintenanceTemplate">
import {
  getDeviceMaintenanceTemplateCustomizeListPage,
  delDeviceMaintenanceTemplate,
} from '@/api/device.js'
import Modify from './modify.vue'
import Detail from './detail.vue'
import { getCustomizeSetting, updateUserCustomize } from '@/api/customize'

const { proxy } = getCurrentInstance()
const emit = defineEmits()

const clearType = ref(true)
const slipSelectRequest = ref(true)
const filterInfo = ref({
  query: {
    sortString: '',
    customizeUserVO: {},
    queryMap: {
      deptAuth: {
        asTbName: 'device_maintenance_template',
        columnName: 'dept_id',
        queryType: 'IN',
        queryCategory: 'AND',
        value: 'deptAuth',
      },
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [
    { type: 'button', label: '查询', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btList: [
    {
      type: 'button',
      label: '新增',
      event: create,
      show: proxy.$permission('deviceMaintenanceTemplate:add'),
    },
    {
      type: 'slot',
      label: '删除',
      value: 'remove',
      show: proxy.$permission('deviceMaintenanceTemplate:del'),
    },
  ],
})
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  selections: [],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '编辑',
        event: update,
        show: proxy.$permission('deviceMaintenanceTemplate:update'),
      },
      {
        label: '复制',
        event: copyData,
        show: proxy.$permission('deviceMaintenanceTemplate:copy'),
      },
      {
        label: '删除',
        event: remove,
        popconfirmText: '删除后将改变数据,是否继续？',
        show: proxy.$permission('deviceMaintenanceTemplate:del'),
      },
    ],
  },
})
//明细相关
const detailInfo = ref({
  visible: false,
  title: '养护模板详情',
  id: '',
})
//新增或更新相关
const modifyInfo = ref({
  visible: false,
  title: '',
  id: '',
  type: ''
})
const preferenceInfo = ref({
  visible: false,
})
onMounted(() => {
  setData()
})
function setData() {
  let obj = {}
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  filterInfo.value.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
}
function update(row) {
  sessionStorage.setItem('storeRowId', row.id)
  modifyInfo.value.id = row.id
  modifyInfo.value.visible = true
  modifyInfo.value.type = 'update'
  modifyInfo.value.title = '修改养护模板'
}
function copyData(row) {
  sessionStorage.setItem('storeRowId', row.id)
  modifyInfo.value.id = row.id
  modifyInfo.value.visible = true
  modifyInfo.value.type = 'copy'
  modifyInfo.value.title = '新增养护模板'
}
// 创建
function create() {
  modifyInfo.value.visible = true
  modifyInfo.value.title = '新增养护模板'
  modifyInfo.value.type = 'add'
  modifyInfo.value.id = null
}
function showDetail(row) {
  sessionStorage.setItem('storeRowId', row.id)
  detailInfo.value.visible = true
  detailInfo.value.title = '养护计划模板'
  detailInfo.value.id = row.id
}
//删除
function remove(row = undefined) {
  const ids = _getIds(row)
  if (row && row.id) {
    delDeviceMaintenanceTemplate(ids).then((resp) => {
      if (resp && resp.success) {
        getList()
      }
    })
  } else {
    proxy
      .$confirm('删除后将改变数据,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
      .then(() => {
        delDeviceMaintenanceTemplate(ids).then((resp) => {
          if (resp && resp.success) {
            getList()
          }
        })
      })
  }
}
function _getIds(row) {
  let ids = []
  if (row && row.id) {
    ids = [row.id]
  } else {
    if (tableInfo.value.selections.length > 0) {
      ids = tableInfo.value.selections.map((row) => row.id)
    }
  }
  return ids
}
function returnModify() {
  if (modifyInfo.value.id) {
    getList()
  } else {
    search()
  }
}
function setPreferenceCallback(obj) {
  updateUserCustomize(obj.customizeUserVO).then((res) => {
    if (res && res.success) {
      getCustomize(!slipSelectRequest.value)
    }
  })
}
async function getList() {
  proxy.$nextTick(() => {
    tableInfo.value.refresh = Math.random()
  })
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
function clearSearch() {
  clearType.value = !clearType.value
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
  search()
}
function getCustomize(_slipSelectRequest = true) {
  let param = {
    tableKey:
      'deviceMaintenanceTemplateList-com.slip.spd.entity.vo.device.maintenance.template.DeviceMaintenanceTemplateVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.value.searchList,
        data.detail
      )
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.value.query.queryMap,
      }
      filterInfo.value.copyQueryMap = proxy.$fn.deepClone(
        filterInfo.value.query.queryMap
      )
      filterInfo.value.searchList = searchInfo.searchList
      filterInfo.value.searchList.forEach((item) => {
        item.event = search
      })
      slipSelectRequest.value = _slipSelectRequest
      tableInfo.value.api = getDeviceMaintenanceTemplateCustomizeListPage
      getList()
    }
  })
}
</script>

<style scoped></style>
